package com.friendsofed.isometric
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	import flash.utils.getDefinitionByName;

	public class MapLoader extends EventDispatcher
	{
		private var _grid:Array;
		private var _loader:URLLoader;
		private var _tileTypes:Object;
		
		public  var mapXML : XML;
		public var mapData:Dictionary;
		
		public function MapLoader()
		{
			 _tileTypes = new Object();
		}

		public function loadMap(url:String,type:String=null):void
		{
			 _loader = new URLLoader();
			 _loader.addEventListener(Event.COMPLETE, onLoad);
			 
			 if(type)
			  {
				  _loader.dataFormat = type;
			  }
			 _loader.load(new URLRequest(url));
		}
 
		  private function onLoad(event:Event):void
		  {
			   _grid = new Array(); 
			   
			   mapXML = new XML(_loader.data);
			   mapData = new Dictionary;
			   
			   for each(var mapInfo : XML in mapXML.elements())
			   {
				   if(mapInfo.name()["localName"] != "isoBox") 
				   {
					   mapData[mapInfo.name()["localName"]] = mapInfo.text().toString();
				   }
				   else
				   {
					   mapData["isoBox"] = new Array;
					   
					   for each(var boxInfo : XML in mapInfo.elements())
					   {
						   var box : Object = new Object;
						   box["id"] = boxInfo.@id.toString();
						   box["isoX"] = Number(boxInfo.@isoX);
						   box["isoZ"] = Number(boxInfo.@isoZ);
						   
						   mapData["isoBox"].push(box);
					   }
				   }
			   }
			dispatchEvent(new Event(Event.COMPLETE));
		}

         public function makeWorld(size:Number):IsoWorld
         {
              //var world:IsoWorld = new IsoWorld();
              var world:IsoWorld;

              return world;
         }
	}
}

